var express = require("express");
var router = express.Router();
const { find, updata } = require("./database");

/* GET home page. */
router.post("/", async function (req, res, next) {
  req.session.user = req.body.email;
  // 去数据库查找是否有相同的
  let user = await find(`accountName===${req.session.user}`);
  if (user.length === 0) {
    res.send({ result: false, reason: "用户不存在" });
  } else {
    let passWord = user[0].passWord;
    if (passWord === req.body.password) {
      let userList = await find("_id>=0");
      await updata({ online: true }, { online: false });
      await updata({ _id: user[0]._id }, { online: true });
      res.send({ result: true, user: req.session.user, userList });
    } else {
      res.send({ result: false, reason: "密码错误" });
    }
  }
});

module.exports = router;
